﻿using System;
using Development.Tools;
using Development.Network.Msg;

namespace Development.Network
{
    public class Routiner : Singleton<Routiner>
    {
        private bool _inMsgQueue = false;
        private MsgAction _action;   //消息处理者

        public bool IsRoutine { get { return _inMsgQueue; } }
        /// <summary>
        /// 
        /// </summary>
        public void Init(bool inMsgQueue,MsgAction action)
        {
            _inMsgQueue = inMsgQueue;
            _action = action;
        }

        public void AddMsgPacket(MsgPacket packet)
        {

                if (_action != null)
                {
                    _action.OnPacket(packet);
                }
        }

       
        public int Routine()
        {
        /*    long tickCount = Global.CurrTime();
            int count = 0;

            while (true)
            {
                try
                {
                    MsgPacket msg = MsgQueue.Instance.DeQueue();
                    if (msg!= null)
                    {
                        if (_action != null)
                        {
                            _action.OnPacket(msg);
                        }
  
                        if (count++ >= 1000)
                        {
                            Log.WriteLog(ELogLevel.LOG_INFO, "本次例程已处理消息1000个，队列中剩余" + MsgQueue.Instance.Count + "个消息等待处理");
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }

                }
                catch (Exception e)
                {
                    Log.WriteLog(ELogLevel.LOG_ERROR, string.Format("{0};info[instance:{1}methed:{2}stack:{3}", e.Message, e.Source, e.TargetSite.Name, e.StackTrace));
                }
            }
           

            long tempnow = Global.CurrTime();

            if (tempnow - tickCount > 200)
            {
                Log.WriteLog(ELogLevel.LOG_INFO, "long loop : " + count.ToString() + " msgs in " + (tempnow - tickCount).ToString() + "ms");
            }*/
            return 0;
        }



    }
}
